package com.youren.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.youren.health.constant.MessageConstant;
import com.youren.health.entity.PageResult;
import com.youren.health.entity.QueryPageBean;
import com.youren.health.entity.Result;
import com.youren.health.pojo.Setmeal;
import com.youren.health.service.SetmealService;
import com.youren.health.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-24 16:52
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @PostMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findByPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String imgName = UUID.randomUUID() + suffix;
        try {
            byte[] bytes = imgFile.getBytes();
            OSSUtil.upload(imgName, bytes);
            Map<String, String> map = new HashMap<String, String>();
            map.put("imgName", imgName);
            map.put("domain", OSSUtil.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, map);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setmealService.add(setmeal, checkgroupIds);
        // 获取redis连接对象
        Jedis jedis = jedisPool.getResource();
        // redis中set集合中保存的元素格式为: 套餐id|操作类型|时间戳
        jedis.sadd("setmeal:static:html", setmeal.getId() + "|1|" + System.currentTimeMillis());
        //关闭连接
        jedis.close();
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        Setmeal setmeal = setmealService.findById(id);
        String domain = OSSUtil.DOMAIN;
        resultMap.put("setmeal", setmeal);
        resultMap.put("domain", domain);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, resultMap);
    }

    @RequestMapping("/findCheckGroupIdList")
    public Result findCheckGroupIdList(Integer id) {
        List<Integer> checkGroupIdList = setmealService.findCheckGroupIdList(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIdList);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setmealService.update(setmeal, checkgroupIds);
        Jedis jedis = jedisPool.getResource();
        jedis.sadd("setmeal:static:html", setmeal.getId() + "|1|" + System.currentTimeMillis());
        jedis.close();
        return new Result(true, MessageConstant.Edit_SETMEAL_SUCCESS);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        setmealService.deleteById(id);
        Jedis jedis = jedisPool.getResource();
        // 操作符0代表删除
        jedis.sadd("setmeal:static:html", id + "|0|" + System.currentTimeMillis());
        jedis.close();
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

}
